
//**********
// Assuming that a fit to a Lorentzian has just been done using the /M flag, return
// the amplitude and its estimated error in a wave.
//  If /M was not used the sigma term will be a NaN or simply wrong.
//
Proc LorentzianAmplitude(results)
	string results	// name of wave to receive results
;
	Variable amp= K1/K3,sig= NaN
	if( exists("CM_K3")==1 )
		sig= amp*sqrt(CM_K1[1]/K1^2 + CM_K3[3]/K3^2 - 2*CM_K3[1]/(K1*K3))
	endif
	$(results)= {amp,sig}
End

//**********
// Assuming that a fit to a Lorentzian has just been done using the /M flag, return
// the FWHM, its estimated error, the area & its estimated error in a wave.
//  If /M was not used the area sigma term will be a NaN or simply wrong.
//
Proc LorentzianFWHM_Area(results)
	string results	// name of wave to receive results
;
	Variable vfwhm,fwhmSig,area,areaSig,sig13= NaN
	if( exists("CM_K1")==1 )
		sig13= CM_K1[3]
	endif
	area= Pi*K1/sqrt(K3)
	areaSig= (Pi/K3)*sqrt(K3*W_sigma[1]^2 + (W_sigma[3]*K1)^2/(4*K3) -K1*sig13)
	vfwhm= 2*sqrt(K3)
	fwhmSig= W_sigma[3]/sqrt(K3)
	$(results)= {vfwhm,fwhmSig,area,areaSig}
End

//**********
// Assuming that a fit to a Gaussian has just been done using the /M flag, return
// the FWHM, its estimated error, the area & its estimated error in a wave.
//  If /M was not used the area sigma term will be a NaN or simply wrong.
//
Proc GaussianFWHM_Area(results)
	string results	// name of wave to receive results
;
	Variable vfwhm,fwhmSig,area,areaSig,sig13= NaN
	if( exists("CM_K1")==1 )
		sig13= CM_K1[3]
	endif
	area= K1*K3*sqrt(Pi)
	areaSig= area*sqrt( (W_sigma[1]/K1)^2 + (W_sigma[3]/K3)^2 +2*sig13/(K1*K3) )
	fwhmSig= 2*sqrt(ln(2))
	vfwhm=fwhmSig* K3
	fwhmSig *= W_sigma[3]
	$(results)= {vfwhm,fwhmSig,area,areaSig}
End



//************
// This macro calculates and appends a Full Width at Half Max bar to the **TOP** graph.  Call this
// right after doing either a Gaussian or Lorentzian fit (It assumes that the K variables are
// still valid.)
// A tag is created showing the fwhm value.  It can also include the area.
// WARNING: if you want area and error then the fit must have been done with the /M flag in order
// to provide the cross sigmas.
// If you use this macro on more than one graph in a given experiment then you will have to provide
// unique wave names for the bar for each graph.  An attempt was made to do this automatically but
// it turned out to be more complex than it was worth.
// REQUIRES: IGOR V1.2;  MakeValueReportString() [TN001];GaussianFWHM_Area();
//						LorentzianFWHM_Area()
//
Macro AppendFWHM(fitType,wfwhm,fwhmUnits,showArea,areaUnits,method)
	variable fitType=1
	prompt fitType,"Fit type:",popup,"Gaussian;Lorentzian"
	string wfwhm="FWHM"
	Prompt wfwhm,"Wave name for width bar:"
	String fwhmUnits=""
	Prompt fwhmUnits,"units for FWHM:"
	variable showArea=2
	Prompt showArea,"",popup,"Don't show area;Show area"
	String areaUnits=""
	Prompt areaUnits,"units for area:"
	variable method=4
	Prompt method,"reporting method:",popup,"b.bbb;b.bbbs.sss;b.bbb {s.sss};b.bbbss"
;
	Silent 1; PauseUpdate
	string tstr
	variable hm,xoff,showError= (method==2)
	variable needAreaError=  (method==2) %& (showArea==2) 

	if( needAreaError )
		if( exists("CM_K0")!=1 )
			Abort "need correlation matrix for area error calc"
		endif
	endif
	
	CheckDisplayed $wfwhm								// are we updating a previous run?
	if( V_Flag==1 )
		Remove $wfwhm
	endif

	Make/O r_tmp

	if(fitType==1)											// Gaussian?
		GaussianFWHM_Area("r_tmp")
		hm= K0+K1/2
		xoff= K2
	else														// Lorentzian
		LorentzianFWHM_Area("r_tmp")
		hm= K0+(K1/K3)/2
		xoff=K2
	endif
	
	Make/o/n=3 $wfwhm=hm
	SetScale/I x,-r_tmp[0]/2+xoff,r_tmp[0]/2+xoff,$wfwhm
	Append $wfwhm

	String/G fwhm_tmp				// need a global string for our "poor man's" pass by reference
	MakeValueReportString(r_tmp[0],r_tmp[1],"fwhm_tmp",method-1,fwhmUnits,1)
	sprintf tstr,"fwhm= %s",fwhm_tmp
	if(showArea==2)
		String stmp
		MakeValueReportString(r_tmp[2],r_tmp[3],"fwhm_tmp",method-1,areaUnits,1)
		sprintf stmp,"\rarea= %s",fwhm_tmp
		tstr += stmp
	endif
	KillStrings  fwhm_tmp
	KillWaves r_tmp
	Tag /A=LB/X=5/Y=12 $wfwhm, pnt2x( $wfwhm, 1), tstr
End

